import {pageMgr} from "../../modules/coreModule/managers/PageManager";
import {MinePage} from "../../pages/mine/MinePage";
import {DiscoverPage} from "../../pages/discover/DiscoverPage";
import {HomePage} from "../../pages/homepage/HomePagePage";
import {MessagePage} from "../../pages/message/MessagePage";
import {PubTaskPage} from "../../pages/pubTask/PubTaskPage";
import {alertMgr} from "../../modules/coreModule/managers/AlertManager";
import {userMgr} from "../../modules/userModule/managers/UserManager";
import {RoleType} from "../../modules/userModule/data/Role";
import {TaskType} from "../../modules/taskModule/data/Task";
import {CoreThrow} from "../../modules/coreModule/CoreCommon";
import {UserThrow} from "../../modules/userModule/UserCommon";

const Pages = [HomePage, DiscoverPage, MessagePage, MinePage];

Component({
	properties: {
		curPageIndex: {
			type: Number,
			value: 0
		},
	},
	data: {
		color: "#333333",
		selectedColor: "#1577ff",
		pages: [{
			pageIdx: 0,
			text: "首页",
			iconPath: "../../assets/tabbar/homepage.png",
			selectedIconPath: "../../assets/tabbar/homepage-selected.png"
		}, {
			pageIdx: 1,
			text: "动态",
			iconPath: "../../assets/tabbar/discover.png",
			selectedIconPath: "../../assets/tabbar/discover-selected.png"
		}, {
			command: "add",
			iconPath: "../../assets/tabbar/main-button1.png",
			selectedIconPath: "../../assets/tabbar/main-button2.png"
		}, {
			pageIdx: 2,
			text: "消息",
			iconPath: "../../assets/tabbar/message.png",
			selectedIconPath: "../../assets/tabbar/message-selected.png"
		}, {
			pageIdx: 3,
			text: "我的",
			iconPath: "../../assets/tabbar/mine.png",
			selectedIconPath: "../../assets/tabbar/mine-selected.png"
		}],

		mainMenuOption:[
			{
				icon:'../../assets/tabbar/icon1.png',
				title:'成长训练营',
				detail:'团建拓展、轰趴、儿童潜力激发及各类活动开展'
			},
			{
				icon:'../../assets/tabbar/icon2.png',
				title:'场地',
				detail:'活动场地信息提供'
			},
			{
				icon:'../../assets/tabbar/icon3.png',
				title:'日常兼职',
				detail:'日结工作或短期雇佣'
			},
			{
				icon:'../../assets/tabbar/icon4.png',
				title:'军事训练',
				detail:'学校军训及其它单位军事训练信息提供'
			},
		],
		showMainMenu: false,
	},

	methods: {
		onTap(e) {
			const data = e.currentTarget.dataset
			const item = data.item;
/* 			if(item.pageIdx == 1){
				alertMgr().showAlert("功能暂未开放，敬请期待")
				return
			} */

			this.setData({ selected: data.index });
			if (item.pageIdx != undefined) {
				pageMgr().switch(Pages[item.pageIdx] as any).then();
				this.setData({ showMainMenu: false })
			} else if (item.command === "add")
				this.setData({
					showMainMenu: !this.data.showMainMenu
				})
		},
		onPubTaskTab(e) {
			const type = e.currentTarget.dataset.index;

			try {
				this.ensureCert(type);
				pageMgr().push(PubTaskPage, {type});
			} catch (e) {
				alertMgr().handleError(e);
			}
		},
		ensureCert(type: TaskType) {
			const user = userMgr().userInfo;
			const roleType = user?.role()?.type;

			switch (type) {
				case TaskType.Military:
				// case TaskType.Camp:
					if (!roleType) UserThrow.CertError("请先进行资质认证！");
					if (![RoleType.School, RoleType.Company].includes(roleType))
						UserThrow.CertError("当前角色不能发布该活动，请先认证为企业或学校");
					break;
				// case TaskType.Camp:
				// 	CoreThrow.SystemError("尚未开放！");
			}
		},
		onClickOverlay(){
			this.setData({showMainMenu: false});
		}
		// currentPageIndex() {
		// 	for (let i = 0; i < this.data.pages.length; i++) {
		// 		const pagePath = this.data.pages[i].pagePath;
		// 		if (pageMgr().isCurPage(pagePath)) return i
		// 	}
		// 	return 0;
		// }
	}
});
